package com.nfc.lock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nfc.lock.common.result.Result;
import com.nfc.lock.dto.request.RoomAddRequest;
import com.nfc.lock.dto.response.RoomDetailVO;
import com.nfc.lock.service.IPowerRoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 配电室Controller
 */
@Tag(name = "配电室管理")
@RestController
@RequestMapping("/api/v1/room")
@RequiredArgsConstructor
public class PowerRoomController {

    private final IPowerRoomService powerRoomService;

    @Operation(summary = "分页查询配电室列表")
    @GetMapping("/list")
    public Result<IPage<RoomDetailVO>> getRoomList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "20") Integer pageSize,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "所属区域ID") @RequestParam(required = false) Long regionId
    ) {
        return Result.success(powerRoomService.getRoomList(pageNum, pageSize, keyword, regionId));
    }

    @Operation(summary = "新增配电室")
    @PostMapping("/add")
    public Result<Void> addRoom(@Valid @RequestBody RoomAddRequest request) {
        powerRoomService.addRoom(request);
        return Result.success();
    }

    @Operation(summary = "更新配电室")
    @PutMapping("/edit/{roomId}")
    public Result<Void> updateRoom(
            @Parameter(description = "配电室ID") @PathVariable Long roomId,
            @Valid @RequestBody RoomAddRequest request
    ) {
        powerRoomService.updateRoom(roomId, request);
        return Result.success();
    }

    @Operation(summary = "删除配电室")
    @DeleteMapping("/delete/{roomId}")
    public Result<Void> deleteRoom(@Parameter(description = "配电室ID") @PathVariable Long roomId) {
        powerRoomService.deleteRoom(roomId);
        return Result.success();
    }

    @Operation(summary = "获取配电室详情")
    @GetMapping({"/detail/{roomId}", "/{roomId}"})
    public Result<RoomDetailVO> getRoomDetail(@Parameter(description = "配电室ID") @PathVariable Long roomId) {
        return Result.success(powerRoomService.getRoomDetail(roomId));
    }
}
